/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.click.app.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.Transient;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 *
 * @author arnold
 */
@Entity
@Table(name = "categorys")
public class Category extends BaseObject implements Serializable {

    private Long id;
    private String name;
    private String nameEs;
    private Date dateCategory;
    private List<Catalog> catalogs;
    private List<ProductType> productTypes;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(name = "date_catagory")
    @Temporal(javax.persistence.TemporalType.DATE)
    public Date getDateCategory() {
        return dateCategory;
    }

    public void setDateCategory(Date dateCategory) {
        this.dateCategory = dateCategory;
    }

    @Override
    public String toString() {
        ToStringBuilder sb = new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE).append("id", this.id).append("name", this.name);
        return sb.toString();
    }

    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Category other = (Category) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
            return false;
        }
        if (this.dateCategory != other.dateCategory && (this.dateCategory == null || !this.dateCategory.equals(other.dateCategory))) {
            return false;
        }
        if (this.productTypes != other.productTypes && (this.productTypes == null || !this.productTypes.equals(other.productTypes))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {

        int hash = 7;
        hash = 29 * hash + (this.id != null ? this.id.hashCode() : 0);
        hash = 29 * hash + (this.name != null ? this.name.hashCode() : 0);
        hash = 29 * hash + (this.dateCategory != null ? this.dateCategory.hashCode() : 0);
        hash = 29 * hash + (this.productTypes != null ? this.productTypes.hashCode() : 0);
        return hash;
    }

    /**
     * @return the productTypes
     */
    @OneToMany(fetch = FetchType.LAZY)
    public List<ProductType> getProductTypes() {
        if (productTypes == null) {
            productTypes = new ArrayList<ProductType>();
        }
        return productTypes;
    }

    @Transient
    public List<String> getPNames() {
        List<String> names = new ArrayList<String>();
        List<ProductType> ps = getProductTypes();
        for (ProductType p : ps) {
            names.add(p.getName());
        }

        return names;
    }

    /**
     * @param productTypes the productTypes to set
     */
    public void setProductTypes(List<ProductType> productTypes) {
        this.productTypes = productTypes;
    }

    /**
     * @return the name
     */
    @Column(name = "name_category")
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the catalogs
     */
    @ManyToMany
    public List<Catalog> getCatalogs() {
        if (catalogs == null) {
            catalogs = new ArrayList<Catalog>();
        }
        return catalogs;
    }

    /**
     * @param catalogs the catalogs to set
     */
    public void setCatalogs(List<Catalog> catalogs) {
        this.catalogs = catalogs;
    }

    /**
     * @return the nameEs
     */
    @Column(name = "name_category_es")
    public String getNameEs() {
        return nameEs;
    }

    /**
     * @param nameEs the nameEs to set
     */
    public void setNameEs(String nameEs) {
        this.nameEs = nameEs;
    }

    @Transient
    public String getNameLang() {
        if (getLanguaje() != null && getLanguaje().equals("es") && getNameEs() != null) {
            return getNameEs();
        }
        return getName();
    }
}
